package com.bf.zk.node;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.bf.zk.constants.Constants;
import com.bf.zk.node.Node;
import com.bf.zk.user.User;
import com.bf.tool.ConfirmInfo;

/**
 * 刻录机节点添加管理界面
 * @author Administrator
 *
 */
public class NodeAdd extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JDialog nodeManagerAdd;
	private JTextField nodeName;
	private JTextField ipAddr;
	@SuppressWarnings("unused")
	private JFrame parent;
	@SuppressWarnings("unused")
	public NodeAdd(JFrame parent, boolean modal) {
		this.parent = parent;
		// 显示设置
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		nodeManagerAdd = new JDialog(parent, "节点增加", true);
		nodeManagerAdd.setSize(400, 240);
		nodeManagerAdd.setDefaultCloseOperation(2);
		// 居中显示
		nodeManagerAdd.setLayout(null);
		nodeManagerAdd.setLocationRelativeTo(parent);
		nodeManagerAdd.setIconImage(Toolkit.getDefaultToolkit().createImage("./pic/Log.jpg"));

		// 节点名称
		JLabel label1 = new JLabel("节点名称：");
		label1.setBounds(50, 10, 80, 30);
		nodeName = new JTextField("");
		nodeName.setBounds(130, 10, 150, 30);

		// IP地址
		JLabel label2 = new JLabel("IP地址：");
		label2.setBounds(50, 70, 80, 30);
		ipAddr = new JTextField("");
		ipAddr.setBounds(130, 70, 150, 30);

		// 节点增加确定
		JButton nodeAdd1 = new JButton("确定");
		nodeAdd1.setActionCommand("nodeAdd1");
		nodeAdd1.setBounds(100, 140, 70, 30);

		// 节点增加取消
		JButton nodeCancel = new JButton("取消");
		nodeCancel.setActionCommand("nodeCancel");
		nodeCancel.setBounds(200, 140, 70, 30);

		nodeManagerAdd.add(label1);
		nodeManagerAdd.add(label2);

		nodeManagerAdd.add(nodeName);
		nodeManagerAdd.add(ipAddr);
		nodeManagerAdd.add(nodeAdd1);
		nodeManagerAdd.add(nodeCancel);

		nodeAdd1.addActionListener(this);
		nodeCancel.addActionListener(this);
		nodeManagerAdd.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// 节点增加确定
		if (e.getActionCommand().equals("nodeAdd1")) {
			//(ConfirmInfo.ConfirmUser(nodeName.getText()))&&
			if ((ConfirmInfo.ConfirmIP(ipAddr.getText()))&&(!(nodeName.getText().isEmpty()))) {
				int rs = -1;
				String fileName = "./sqlite/znzk.db";
				try {
					Class.forName("org.sqlite.JDBC");
					Connection conn = DriverManager.getConnection("jdbc:sqlite:" + fileName);
					Statement stmt = conn.createStatement();
					boolean rs0 = stmt.execute("INSERT INTO znzk_node (nodename,nodeip) VALUES ('"+ nodeName.getText() + "','"+ ipAddr.getText() + "')");
					if (rs0) {
						JOptionPane.showMessageDialog(nodeManagerAdd, "添加失败!",	"警告", JOptionPane.WARNING_MESSAGE);
					}else {
						ResultSet rs1 = stmt
								.executeQuery("SELECT nodeid FROM znzk_node where nodename='" + nodeName.getText() + "'");
						Constants.addNode=new Node();
						Constants.addNode.setChooseNode(false);
						Constants.addNode.setNodeId("nodeid");
						Constants.addNode.setName(nodeName.getText());
						Constants.addNode.setIPAdd(ipAddr.getText());
						Constants.nodeOrNot=true;
						nodeManagerAdd.setVisible(false);
					}
					conn.close();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			} else if (!(ConfirmInfo.ConfirmIP(ipAddr.getText())))  {
				JOptionPane.showMessageDialog(nodeManagerAdd, "IP不符合规则!","警告", JOptionPane.WARNING_MESSAGE);
				//nodeName.setText("");
				//ipAddr.setText("");
			}else {
				JOptionPane.showMessageDialog(nodeManagerAdd, "节点名、IP不符合规则!","警告", JOptionPane.WARNING_MESSAGE);
				//nodeName.setText("");
				//ipAddr.setText("");
			}
		}

		// 节点增加取消
		if (e.getActionCommand().equals("nodeCancel")) {
			Constants.nodeOrNotCan=true;
			nodeManagerAdd.setVisible(false);
		}
	}
}
